﻿/// <summary>
/// https://leetcode-cn.com/problems/wildcard-matching/
/// </summary>
public class Solution
{
    public bool IsMatch(string s, string p)
    {
        int sIndex = 0;
        int pIndex = 0;
        int starringStart = -1;
        int starring = -1;
        for (; sIndex < s.Length;)
        {
            if (pIndex < p.Length)
            {
                var currentP = p[pIndex];
                if (s[sIndex] == p[pIndex] || currentP == '?')
                {
                    sIndex++;
                    pIndex++;
                    continue;
                }

                if (currentP == '*')
                {
                    starringStart = pIndex++;
                    starring = sIndex;
                    continue;
                }
            }

            if (starringStart != -1)
            {
                sIndex = ++starring;
                pIndex = starringStart + 1;
                continue;
            }

            return false;
        }

        for (; pIndex < p.Length; pIndex++)
        {
            if (p[pIndex] != '*')
                return false;
        }
        return true;
    }
}

/* 下方案超时
using System.Text;
using System.Text.RegularExpressions;
/// <summary>
/// https://leetcode-cn.com/problems/wildcard-matching/
/// </summary>
public class Solution
{
    public bool IsMatch(string s, string p)
    {
        StringBuilder stringBuilder = new StringBuilder().Append('^');
        bool lastIsStar = false;
        foreach(var c in p)
        {
            if (c is '*')
            {
                if (!lastIsStar)
                {
                    lastIsStar = true;
                    _ = stringBuilder.Append(".*");
                }
            }
            else
            {
                lastIsStar = false;
                _ = stringBuilder.Append(c is '?' ? '.' : c);
            }
        }

        return Regex.IsMatch(s, stringBuilder.Append('$').ToString());
    }
}
*/